package com.ourteam.product.model.api;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.ourteam.frame.web.AbstractSearchModel;
import com.ourteam.product.model.dao.IProductModelObjectRelateDAO;
import com.ourteam.product.model.dao.ProductModelObjectRelateDAOQueryBean;
import com.ourteam.product.model.domain.ProductModelObjectRelateBean;

public class ProductModelObjectRelatedObjectSearchModel
		extends AbstractSearchModel<ProductModelObjectRelateDAOQueryBean> {
	
	private long objectId;
	
	private long relatedObjectId;
	
	private String relatedType;
	

	public long getObjectId() {
		return objectId;
	}

	public void setObjectId(long objectId) {
		this.objectId = objectId;
	}

	public String getRelatedType() {
		return relatedType;
	}

	public void setRelatedType(String relatedType) {
		this.relatedType = relatedType;
	}
	
	

	public long getRelatedObjectId() {
		return relatedObjectId;
	}

	public void setRelatedObjectId(long relatedObjectId) {
		this.relatedObjectId = relatedObjectId;
	}

	@Override
	public String getMappedDAOField(String aProperty) {
		return ProductModelObjectRelateBean.getMappedDAOField(aProperty);
	}

	@Override
	protected void doBuildQueryBean(ProductModelObjectRelateDAOQueryBean queryBean) throws Exception {
		
		ProductModelObjectRelateDAOQueryBean.Criteria criteria = queryBean.createCriteria();
		
		if(objectId > 0){
			criteria.andObjectIdEqualTo(objectId);
		}
		
		if(relatedObjectId > 0){
			criteria.andRelatedObjectIdEqualTo(relatedObjectId);
		}
		
		if(StringUtils.isNotBlank(relatedType)){
			String[] relatedTypes = StringUtils.split(relatedType,",");
			if(ArrayUtils.isEmpty(relatedTypes)){
				criteria.andRelatedTypeEqualTo(relatedType);
			}
			else{
				criteria.andRelatedTypeIn(relatedTypes);
			}
			
		}
		
		queryBean.setSortProperty(IProductModelObjectRelateDAO.SortIndex);

	}

}
